package com.hs.carrental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.carrental.dto.order.OrderPageDto;
import com.hs.carrental.dto.order.OrderSaveDto;
import com.hs.carrental.entity.Car;
import com.hs.carrental.entity.Order;
import com.hs.carrental.mapper.CarMapper;
import com.hs.carrental.mapper.OrderMapper;
import com.hs.carrental.service.ICarService;
import com.hs.carrental.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.carrental.util.Helper;
import com.hs.carrental.vo.order.OrderPageVo;
import com.hs.carrental.vo.order.OrderStatChartVo;
import com.hs.carrental.vo.order.OrderStatVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ICarService carService;

    /**
     * 新增或编辑
     * @param orderSaveDto
     * @return
     */
    @Override
    public boolean save(OrderSaveDto orderSaveDto) {
        Order order = new Order();
        BeanUtils.copyProperties(orderSaveDto,order);
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper<>();
        //把车辆状态改为已出租
        if (orderSaveDto.getOrderState().equals(1)||orderSaveDto.getOrderState().equals(2)){
            updateWrapper.set("state",1).eq("carId",orderSaveDto.getCarId());
            carService.update(updateWrapper);
        } else if (orderSaveDto.getOrderState().equals(0)||orderSaveDto.getOrderState().equals(3)||orderSaveDto.getOrderState().equals(4)){
            //把车辆状态改为未出租
            updateWrapper.set("state",0).eq("carId",orderSaveDto.getCarId());
            carService.update(updateWrapper);
        }
        return this.saveOrUpdate(order);
    }

    /**
     * 分页查询
     * @param orderPageDto
     * @return
     */
    @Override
    public IPage<OrderPageVo> getPage(OrderPageDto orderPageDto) {
        Page<Order> page = new Page<>(orderPageDto.getPageNum(),orderPageDto.getPageSize());
        IPage<OrderPageVo> iPage = orderMapper.getPage(page, orderPageDto.getOrderCode(), orderPageDto.getCarNumber(), orderPageDto.getStoreId(),orderPageDto.getCustomerAccount());
        return iPage;
    }

    /**
     * 按门店查询订单统计情况
     * @param storeId
     * @return
     */
    @Override
    public List<OrderStatChartVo> statOrderByStore(Integer storeId,String year) {
        if (Helper.isEmpty(year)){
            year = String.valueOf(LocalDate.now().getYear());
        }
        List<OrderStatVo> orderStatVos = orderMapper.statOrderByStore(storeId, year);
        List<OrderStatChartVo> result = new ArrayList<>();
        result = orderStatVos.stream().map(orderStatVo -> {
            OrderStatChartVo orderStatChartVo = new OrderStatChartVo();
            LinkedList<Long> total = new LinkedList<>();
            orderStatChartVo.setStoreName(orderStatVo.getStoreName());
            total.add(orderStatVo.getJanuary());
            total.add(orderStatVo.getFebruary());
            total.add(orderStatVo.getMarch());
            total.add(orderStatVo.getApril());
            total.add(orderStatVo.getMay());
            total.add(orderStatVo.getJune());
            total.add(orderStatVo.getJuly());
            total.add(orderStatVo.getAugust());
            total.add(orderStatVo.getSeptember());
            total.add(orderStatVo.getOctober());
            total.add(orderStatVo.getNovember());
            total.add(orderStatVo.getDecember());
            orderStatChartVo.setChartData(total);
            return orderStatChartVo;
        }).collect(Collectors.toList());
        return result;
    }

    /**
     * 删除
     * @param orderId
     * @return
     */
    @Override
    public boolean delete(Integer orderId) {
        //删除订单
        Order byId = this.getById(orderId);
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("state",0).eq("carId",byId.getCarId());
        //将该订单车辆状态改为0
        carService.update(updateWrapper);
        return this.removeById(orderId);
    }
}
